package com.lorenzogao.lib_view

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import com.lorenzogao.lib_view.ext.px2dp
import com.lorenzogao.lib_view.ext.sp2px
import kotlin.math.abs

/**
 *  create by LorenzoGao
 *  email: 2508719070@qq.com
 *  TIME: 2019-11-13 10:48
 *  description:圆形进度条
 */

class RoundProgressView : View {

    private val defalutWidthAndHegiht = 100

    private var mOuterColor = Color.BLUE

    private var mInnerColor = Color.RED

    private var mTextSize = 16

    private var mTextColor = Color.RED

    private var mBorderWidth = 10

    private var mProgress = 0f

    private val mOuterPaint by lazy {
        val paint = Paint()
        paint.isAntiAlias = true
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = mBorderWidth.toFloat()
        paint.color = mOuterColor
        paint

    }

    private val mInnnerPaint by lazy {
        val paint = Paint()
        paint.isAntiAlias = true
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = mBorderWidth.toFloat()
        paint.color = mInnerColor
        paint

    }

    private val mTextPaint by lazy {
        val paint = Paint()
        paint.isAntiAlias = true
        paint.textSize = mTextSize.sp2px(context).toFloat()
        paint.color = mTextColor
        paint

    }


    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {

        val array = context?.obtainStyledAttributes(attrs, R.styleable.RoundProgressView)

        array?.apply {
            mBorderWidth = getInteger(R.styleable.RoundProgressView_progressBorderWidth, mBorderWidth)
            mOuterColor = getColor(R.styleable.RoundProgressView_progressOuterColor, mOuterColor)
            mInnerColor = getColor(R.styleable.RoundProgressView_progressInnerColor, mInnerColor)
            mTextSize = getDimensionPixelSize(R.styleable.RoundProgressView_progressTextSize, mTextSize)
            mTextColor = getColor(R.styleable.RoundProgressView_progressTextColor, mTextColor)
            recycle()
        }


    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
        val heightMode = MeasureSpec.getMode(heightMeasureSpec)
        var width = MeasureSpec.getSize(widthMeasureSpec)
        var height = MeasureSpec.getSize(heightMeasureSpec)



        if (widthMode == MeasureSpec.AT_MOST) {
            width = defalutWidthAndHegiht.px2dp(context)
        }
        if (heightMode == MeasureSpec.AT_MOST) {
            height = defalutWidthAndHegiht.px2dp(context)
        }

        setMeasuredDimension(if (width > height) height else width, if (width > height) height else width)

    }


    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)


        val rectF = RectF(
            mBorderWidth.toFloat(),
            mBorderWidth.toFloat(),
            width - mBorderWidth.toFloat(),
            height - mBorderWidth.toFloat()
        )


        canvas.drawArc(rectF, 0f, 360f, false, mOuterPaint)


        canvas.drawArc(rectF, 0f, (360 * mProgress), false, mInnnerPaint)


        val centen = width / 2

        val text = "${(mProgress * 100).toInt()}%"

        val bounds = Rect()
        mTextPaint.getTextBounds(text, 0, text.length, bounds)
        val dx = centen - bounds.width() / 2

        val fontMetrics = mTextPaint.fontMetrics

        val dy = (abs(fontMetrics.ascent) + fontMetrics.descent) / 2 - fontMetrics.descent
        val baseLine = dy + height / 2
        canvas.drawText(text, dx.toFloat(), baseLine, mTextPaint)




    }

     fun setProgress(progress: Float) {
        this.mProgress = progress
        invalidate()
    }

}